package com.lvy.hczn.transmit.controller;

import com.lvy.hczn.front.common.annotation.Log;
import com.lvy.hczn.front.common.core.domain.AjaxResult;
import com.lvy.hczn.front.common.enums.BusinessType;
import com.lvy.hczn.front.common.exception.UtilException;
import com.lvy.hczn.front.system.domain.business.Category;
import com.lvy.hczn.mq.rocket.ParamInfo;
import com.lvy.hczn.front.system.service.CategoryService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RequestMapping("/api/dt/cate/")
@RestController
@Slf4j
public class TCategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 商品分类同步列表
     *
     * @param info
     * @return
     */
    @ApiOperation(value = "商品分类同步", notes = "商品分类同步")
    @PostMapping("batch")
    public AjaxResult syncCategoryBatch(HttpServletRequest request, @RequestBody ParamInfo info) {
        try {
            categoryService.syncCategoryBatch(request, info, true);
            return AjaxResult.success();
        } catch (Exception e) {
            if (e instanceof UtilException) {
                return AjaxResult.error(e.getMessage());
            } else {
                log.error("syncCategoryBatch同步异常：{}", e);
                return AjaxResult.error("系统异常");
            }
        }
    }

    /**
     * 商品分类同步
     *
     * @param info
     * @return
     */
    @ApiOperation(value = "商品分类同步", notes = "商品分类同步")
    @PostMapping("category")
    public AjaxResult syncCategory(HttpServletRequest request, @RequestBody ParamInfo<Category> info) {
        try {
            categoryService.syncCategory(request, info, true);
            return AjaxResult.success();
        } catch (Exception e) {
            if (e instanceof UtilException) {
                return AjaxResult.error(e.getMessage());
            } else {
                log.error("syncCategory同步异常：{}", e);
                return AjaxResult.error("系统异常");
            }
        }
    }

    @Log(title = "同步到仓库的分类列表", businessType = BusinessType.OTHER)
    @PostMapping("syncCategoryToWarehouse")
    @ResponseBody
    public AjaxResult syncCategoryToWarehouse() {
        return AjaxResult.success(categoryService.list());
    }
}
